# In [1]: import torch

# In [2]: a = torch.randn(2,3)

# In [3]: a.type()
# Out[3]: 'torch.FloatTensor'

# In [4]: type(a)
# Out[4]: torch.Tensor

# In [5]: isinstance(a, torch.FloatTensor)
# Out[5]: True

# In [7]: torch.tensor(1)
# Out[7]: tensor(1)

# In [8]: torch.tensor(1.3)
# Out[8]: tensor(1.3000)

# In [9]: a = torch.tensor(2.2)

# In [10]: a.shape
# Out[10]: torch.Size([])

# In [11]: len(a.shape)
# Out[11]: 0

# In [12]: a.size()
# Out[12]: torch.Size([])

# In [13]: torch.tensor([1.1])
# Out[13]: tensor([1.1000])

# In [14]: torch.tensor([1.1,2.2])
# Out[14]: tensor([1.1000, 2.2000])

# In [15]: torch.FloatTensor(1)
# Out[15]: tensor([0.])

# In [16]: torch.FloatTensor(1)
# Out[16]: tensor([0.])

# In [17]: torch.FloatTensor(1)
# Out[17]: tensor([0.])

# In [18]: torch.FloatTensor(2)
# Out[18]: tensor([-1.2501e+11,  6.6281e-43])

# In [19]: torch.FloatTensor(2)
# Out[19]: tensor([0., 0.])

# In [20]: torch.FloatTensor(2)
# Out[20]: tensor([0., 0.])

# In [21]: torch.FloatTensor(2)
# Out[21]: tensor([0., 0.])

# In [22]: torch.FloatTensor(2)
# Out[22]: tensor([0., 0.])

# In [23]: import numpy as np

# In [24]: data = np.ones(2)

# In [25]: data
# Out[25]: array([1., 1.])

# In [26]: torch.from_numpy(data)
# Out[26]: tensor([1., 1.], dtype=torch.float64)

# In [27]: a = torch.ones(2)

# In [28]: a.shape
# Out[28]: torch.Size([2])

# In [29]: a.size()
# Out[29]: torch.Size([2])

# In [30]: a
# Out[30]: tensor([1., 1.])

# In [31]: a = torch.randn(2,3)

# In [32]: a
# Out[32]: 
# tensor([[ 1.1326, -0.1183, -1.6966],
#         [-0.2386,  0.9654,  0.3074]])

# In [33]: a.size(0)
# Out[33]: 2

# In [34]: a.size(1)
# Out[34]: 3

# In [35]: a.shape
# Out[35]: torch.Size([2, 3])

# In [36]: a.shape[1]
# Out[36]: 3

# In [37]: a.shape[0]
# Out[37]: 2

# In [38]: a=torch.rand(1,2,3)

# In [39]: a
# Out[39]: 
# tensor([[[0.4345, 0.2577, 0.2636],
#          [0.4444, 0.4020, 0.5979]]])

# In [40]: a.shape
# Out[40]: torch.Size([1, 2, 3])

# In [41]: a[0]
# Out[41]: 
# tensor([[0.4345, 0.2577, 0.2636],
#         [0.4444, 0.4020, 0.5979]])

# In [42]: list(a.shape)
# Out[42]: [1, 2, 3]

# In [43]: a=torch.rand(2,3,28,28)

# In [44]: a
# Out[44]: 
# tensor([[[[0.5772, 0.9167, 0.3719,  ..., 0.1548, 0.5123, 0.1457],
#           [0.6649, 0.9150, 0.9326,  ..., 0.7961, 0.7860, 0.8883],
#           [0.7273, 0.6908, 0.8014,  ..., 0.2497, 0.7231, 0.6194],
#           ...,
#           [0.7735, 0.6688, 0.5069,  ..., 0.6508, 0.7037, 0.3972],
#           [0.2423, 0.8782, 0.2987,  ..., 0.4071, 0.1588, 0.8158],
#           [0.9455, 0.6314, 0.3096,  ..., 0.7669, 0.8157, 0.4492]],

#          [[0.8101, 0.2254, 0.1904,  ..., 0.0109, 0.1868, 0.6993],
#           [0.0324, 0.8703, 0.3353,  ..., 0.7371, 0.8714, 0.0208],
#           [0.0117, 0.3928, 0.3545,  ..., 0.8792, 0.8060, 0.2076],
#           ...,
#           [0.5500, 0.0067, 0.9311,  ..., 0.6940, 0.6103, 0.9500],
#           [0.3329, 0.9337, 0.2431,  ..., 0.1435, 0.4999, 0.7874],
#           [0.2777, 0.2152, 0.7264,  ..., 0.1118, 0.5203, 0.3889]],

#          [[0.5829, 0.1181, 0.5414,  ..., 0.7729, 0.1373, 0.1257],
#           [0.1737, 0.3181, 0.6077,  ..., 0.7550, 0.3013, 0.4430],
#           [0.4403, 0.1644, 0.6702,  ..., 0.7610, 0.1534, 0.7023],
#           ...,
#           [0.4881, 0.9849, 0.1355,  ..., 0.7747, 0.4888, 0.8696],
#           [0.2313, 0.4262, 0.9567,  ..., 0.9363, 0.5477, 0.7793],
#           [0.2847, 0.7230, 0.5299,  ..., 0.1941, 0.4954, 0.3021]]],


#         [[[0.9673, 0.1193, 0.0577,  ..., 0.4592, 0.4829, 0.4429],
#           [0.9296, 0.9897, 0.9407,  ..., 0.1053, 0.7597, 0.2653],
#           [0.9292, 0.6586, 0.5508,  ..., 0.6544, 0.6397, 0.2196],
#           ...,
#           [0.6279, 0.4186, 0.3431,  ..., 0.0457, 0.4587, 0.0700],
#           [0.7135, 0.5822, 0.7784,  ..., 0.6888, 0.9310, 0.2133],
#           [0.7947, 0.4695, 0.6713,  ..., 0.2923, 0.6944, 0.4602]],

#          [[0.5083, 0.9022, 0.2723,  ..., 0.6939, 0.7268, 0.7086],
#           [0.3259, 0.6815, 0.7608,  ..., 0.2702, 0.5845, 0.1182],
#           [0.6746, 0.6286, 0.6433,  ..., 0.0488, 0.5304, 0.3636],
#           ...,
#           [0.1163, 0.5038, 0.0909,  ..., 0.3371, 0.7601, 0.8571],
#           [0.3250, 0.4302, 0.6601,  ..., 0.9527, 0.8962, 0.9555],
#           [0.0540, 0.5709, 0.1879,  ..., 0.2218, 0.1912, 0.5771]],

#          [[0.2121, 0.2220, 0.7299,  ..., 0.2057, 0.2766, 0.2996],
#           [0.4515, 0.9689, 0.4214,  ..., 0.4703, 0.0549, 0.9037],
#           [0.0033, 0.0724, 0.1211,  ..., 0.0140, 0.4853, 0.1963],
#           ...,
#           [0.6174, 0.9659, 0.6875,  ..., 0.7470, 0.2842, 0.6953],
#           [0.1886, 0.3197, 0.0952,  ..., 0.8325, 0.5594, 0.6675],
#           [0.4295, 0.3163, 0.9836,  ..., 0.9315, 0.7898, 0.0157]]]])

# In [45]: a.shape
# Out[45]: torch.Size([2, 3, 28, 28])

# In [46]: a.numel()
# Out[46]: 4704

# In [47]: len(a.shape)
# Out[47]: 4

# In [48]: a.dim()
# Out[48]: 4

# In [49]: a=torch.tensor(1)

# In [50]: a.dim()
# Out[50]: 0

# In [51]: a=np.array([2,3.3])

# In [52]: torch.from_numpy(a)
# Out[52]: tensor([2.0000, 3.3000], dtype=torch.float64)

# In [53]: a=np.ones([2,3])

# In [54]: torch.from_numpy(a)
# Out[54]: 
# tensor([[1., 1., 1.],
#         [1., 1., 1.]], dtype=torch.float64)

# In [55]: b=np.array([2,3])

# In [56]: torch.from_numpy(a)
# Out[56]: 
# tensor([[1., 1., 1.],
#         [1., 1., 1.]], dtype=torch.float64)

# In [57]: torch.from_numpy(b)
# Out[57]: tensor([2, 3], dtype=torch.int32)

# In [58]: torch.tensor([2,3.2])
# Out[58]: tensor([2.0000, 3.2000])

# In [59]: torch.FloatTensor([2,3.2])
# Out[59]: tensor([2.0000, 3.2000])

# In [60]: torch.FloatTensor(2,3)
# Out[60]: 
# tensor([[0.0000e+00, 0.0000e+00, 1.4013e-45],
#         [0.0000e+00, 1.4013e-45, 0.0000e+00]])

# In [61]: torch.tensor([[2,3.2],[1,22.3]])
# Out[61]: 
# tensor([[ 2.0000,  3.2000],
#         [ 1.0000, 22.3000]])

# In [62]: torch.empty(2,3)
# Out[62]: 
# tensor([[0.0000e+00, 0.0000e+00, 1.4013e-45],
#         [0.0000e+00, 1.4013e-45, 0.0000e+00]])

# In [63]: torch.empty([2,3])
# Out[63]: 
# tensor([[1.5554e-43, 1.3593e-43, 1.4013e-45],
#         [0.0000e+00, 1.4013e-45, 0.0000e+00]])

# In [65]: torch.IntTensor(2,3)
# Out[65]: 
# tensor([[0, 0, 1],
#         [0, 1, 0]], dtype=torch.int32)

# In [66]: torch.empty(1)
# Out[66]: tensor([0.])

# In [67]: torch.Tensor(2,3)
# Out[67]: 
# tensor([[0.0000e+00, 0.0000e+00, 1.4013e-45],
#         [0.0000e+00, 1.4013e-45, 0.0000e+00]])

# In [68]: torch.IntTensor(2,3)
# Out[68]: 
# tensor([[0, 0, 6],
#         [0, 1, 0]], dtype=torch.int32)

# In [69]: torch.FloatTensor(2,3)
# Out[69]: 
# tensor([[0., 0., 0.],
#         [0., 0., 0.]])

# In [70]: torch.tensor([1.2,3]).type()
# Out[70]: 'torch.FloatTensor'

# In [71]: torch.set_default_tensor_type(torch.DoubleTensor)

# In [72]: torch.tensor([1.2,3]).type()
# Out[72]: 'torch.DoubleTensor'

# In [66]: torch.empty(1)
# Out[66]: tensor([0.])

# In [67]: torch.Tensor(2,3)
# Out[67]: 
# tensor([[0.0000e+00, 0.0000e+00, 1.4013e-45],
#         [0.0000e+00, 1.4013e-45, 0.0000e+00]])

# In [68]: torch.IntTensor(2,3)
# Out[68]: 
# tensor([[0, 0, 6],
#         [0, 1, 0]], dtype=torch.int32)

# In [69]: torch.FloatTensor(2,3)
# Out[69]: 
# tensor([[0., 0., 0.],
#         [0., 0., 0.]])

# In [70]: torch.tensor([1.2,3]).type()
# Out[70]: 'torch.FloatTensor'

# In [71]: torch.set_default_tensor_type(torch.DoubleTensor)

# In [72]: torch.tensor([1.2,3]).type()
# Out[72]: 'torch.DoubleTensor'

# In [73]: torch.rand(3,3)
# Out[73]: 
# tensor([[0.8647, 0.4121, 0.6167],
#         [0.0377, 0.0837, 0.7995],
#         [0.6325, 0.4872, 0.3690]])

# In [74]: a=torch.rand(3,3)

# In [75]: torch.rand_like(a)
# Out[75]: 
# tensor([[0.4489, 0.6747, 0.1764],
#         [0.3073, 0.5243, 0.4022],
#         [0.7316, 0.1569, 0.8171]])

# In [76]: torch.randint(1,10,[3,3])
# Out[76]: 
# tensor([[8, 7, 1],
#         [4, 8, 5],
#         [2, 4, 1]])

# In [77]: torch.randn(3,3)
# Out[77]: 
# tensor([[-0.7576, -2.0369, -2.4460],
#         [-1.6012, -0.1828,  2.2803],
#         [-1.1803, -0.7647, -0.5069]])

# In [79]: torch.normal(mean=torch.full([10],0), std=torch.arange(1,0,-0.1))
# Out[79]: 
# tensor([-2.0689e+00,  1.8909e+00,  5.5269e-01,  5.2028e-01,  5.6791e-01,
#         -2.8417e-01, -1.5159e-01,  2.1073e-01, -2.0007e-03, -4.1603e-02])

# In [80]: torch.normal(mean=torch.full([10],0), std=torch.arange(1,0,-0.1))
# Out[80]: 
# tensor([ 3.3250e-04, -6.9714e-01,  2.8683e-01,  2.5858e-01,  3.1652e-01,
#         -1.9535e-01,  3.8682e-01, -4.5524e-01,  1.3340e-01,  1.0932e-02])

# In [81]: torch.full([2,3], 7)
# Out[81]: 
# tensor([[7., 7., 7.],
#         [7., 7., 7.]])

# In [82]: torch.full([],7)
# Out[82]: tensor(7.)

# In [83]: torch.full([1],7)
# Out[83]: tensor([7.])

# In [84]: torch.arange(0,10)
# Out[84]: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# In [85]: torch.arange(0,10,2)
# Out[85]: tensor([0, 2, 4, 6, 8])

# In [87]: torch.linspace(0,10,steps=4)
# Out[87]: tensor([ 0.0000,  3.3333,  6.6667, 10.0000])

# In [88]: torch.linspace(0,10,steps=10)
# Out[88]: 
# tensor([ 0.0000,  1.1111,  2.2222,  3.3333,  4.4444,  5.5556,  6.6667,  7.7778,
#          8.8889, 10.0000])

# In [89]: torch.linspace(0,10,steps=11)
# Out[89]: tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])

# In [90]: torch.logspace(0,-1,steps=10)
# Out[90]: 
# tensor([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292,
#         0.1000])

# In [91]: torch.logspace(0,1,steps=10)
# Out[91]: 
# tensor([ 1.0000,  1.2915,  1.6681,  2.1544,  2.7826,  3.5938,  4.6416,  5.9948,
#          7.7426, 10.0000])

# In [93]: torch.logspace(0,1,steps=2)
# Out[93]: tensor([ 1., 10.])

# In [94]: torch.logspace(0,1,steps=10,base=2)
# Out[94]: 
# tensor([1.0000, 1.0801, 1.1665, 1.2599, 1.3608, 1.4697, 1.5874, 1.7145, 1.8517,
#         2.0000])

# In [95]: torch.ones(3,3)
# Out[95]: 
# tensor([[1., 1., 1.],
#         [1., 1., 1.],
#         [1., 1., 1.]])

# In [96]: torch.zeros(3,3)
# Out[96]: 
# tensor([[0., 0., 0.],
#         [0., 0., 0.],
#         [0., 0., 0.]])

# In [97]: torch.eye(3,4)
# Out[97]: 
# tensor([[1., 0., 0., 0.],
#         [0., 1., 0., 0.],
#         [0., 0., 1., 0.]])

# In [98]: torch.eye(3)
# Out[98]: 
# tensor([[1., 0., 0.],
#         [0., 1., 0.],
#         [0., 0., 1.]])

# In [99]: a=torch.zeros(3,3)

# In [100]: torch.ones_like(a)
# Out[100]: 
# tensor([[1., 1., 1.],
#         [1., 1., 1.],
#         [1., 1., 1.]])

# In [101]: torch.randperm(10)
# Out[101]: tensor([9, 2, 1, 5, 4, 3, 0, 7, 8, 6])

# In [102]: a=torch.rand(2,3)

# In [103]: b=torch.rand(2,2)

# In [104]: idx=torch.randperm(2)

# In [105]: idx
# Out[105]: tensor([0, 1])

# In [106]: idx
# Out[106]: tensor([0, 1])

# In [107]: idx
# Out[107]: tensor([0, 1])

# In [108]: a[idx]
# Out[108]: 
# tensor([[0.0048, 0.6340, 0.7914],
#         [0.6208, 0.1485, 0.4941]])

# In [109]: b[idx]
# Out[109]: 
# tensor([[0.7325, 0.3386],
#         [0.7120, 0.6926]])

# In [110]: a,b
# Out[110]: 
# (tensor([[0.0048, 0.6340, 0.7914],
#          [0.6208, 0.1485, 0.4941]]), tensor([[0.7325, 0.3386],
#          [0.7120, 0.6926]]))

# 索引与切片
# In [111]: a=torch.rand(4,3,28,28)

# In [112]: a[0].shape
# Out[112]: torch.Size([3, 28, 28])

# In [113]: a[0,0].shape
# Out[113]: torch.Size([28, 28])

# In [114]: a[0,0,2,4]
# Out[114]: tensor(0.4114)

# In [115]: a.shape
# Out[115]: torch.Size([4, 3, 28, 28])

# In [116]: a[:2].shape
# Out[116]: torch.Size([2, 3, 28, 28])

# In [117]: a[:2,:1,:,:].shape
# Out[117]: torch.Size([2, 1, 28, 28])

# In [118]: a[:2,1:,:,:].shape
# Out[118]: torch.Size([2, 2, 28, 28])

# In [119]: a[:2,-1:,:,:].shape
# Out[119]: torch.Size([2, 1, 28, 28])

# In [120]: a[:,:,0:28:2,0:28:2].shape
# Out[120]: torch.Size([4, 3, 14, 14])

# In [121]: a[:,:,::2,::2].shape
# Out[121]: torch.Size([4, 3, 14, 14])

# In [125]: a.index_select(0,torch.tensor([0,2])).shape
# Out[125]: torch.Size([2, 3, 28, 28])

# In [126]: a.index_select(0,torch.tensor([1,2])).shape
# Out[126]: torch.Size([2, 3, 28, 28])

# In [127]: a.index_select(2,torch.arange(8)).shape
# Out[127]: torch.Size([4, 3, 8, 28])

# In [128]: a[...].shape
# Out[128]: torch.Size([4, 3, 28, 28])

# In [129]: a[0,...].shape
# Out[129]: torch.Size([3, 28, 28])

# In [130]: a[:,1,...].shape
# Out[130]: torch.Size([4, 28, 28])

# In [131]: a[...,:2].shape
# Out[131]: torch.Size([4, 3, 28, 2])

# In [132]: x = torch.randn(3,4)

# In [133]: x
# Out[133]: 
# tensor([[ 0.8922, -0.2922,  0.8387,  0.6128],
#         [-0.2510,  0.1639,  0.0175,  0.6212],
#         [-0.7664,  0.2408,  0.9386,  0.2261]])

# In [134]: mask = x.ge(0.5)

# In [135]: mask
# Out[135]: 
# tensor([[ True, False,  True,  True],
#         [False, False, False,  True],
#         [False, False,  True, False]])

# In [136]: torch.masked_select(x,mask)
# Out[136]: tensor([0.8922, 0.8387, 0.6128, 0.6212, 0.9386])

# In [137]: torch.masked_select(x,mask).shape
# Out[137]: torch.Size([5])

# In [138]: src = torch.tensor([[4,3,5],[6,7,8]])

# In [139]: torch.take(src,torch.tensor([0,2,5]))
# Out[139]: tensor([4, 5, 8])

# In [140]: torch.take(src,torch.tensor([0,2,-1]))
# Out[140]: tensor([4, 5, 8])

# In [141]: a=torch.rand(4,1,28,28)

# In [142]: a.shape
# Out[142]: torch.Size([4, 1, 28, 28])

# In [143]: a.view(4,28*28)
# Out[143]: 
# tensor([[0.2852, 0.9806, 0.3499,  ..., 0.3306, 0.8866, 0.6955],
#         [0.1219, 0.1756, 0.4761,  ..., 0.7023, 0.2581, 0.2889],
#         [0.8096, 0.6748, 0.7965,  ..., 0.4438, 0.3549, 0.1279],
#         [0.2666, 0.8763, 0.5001,  ..., 0.5188, 0.0443, 0.0872]])


# In [144]: a.view(4,28*28).shape
# Out[144]: torch.Size([4, 784])

# In [145]: a.view(4*1,28, 28).shape
# Out[145]: torch.Size([4, 28, 28])

# In [146]: b=a.view(4,784)

# In [147]: b.view(4,28,28,1)

# unsqueeze
# In [148]: a.shape
# Out[148]: torch.Size([4, 1, 28, 28])

# In [149]: a.unsqueeze(0).shape
# Out[149]: torch.Size([1, 4, 1, 28, 28])

# In [150]: a.unsqueeze(-1).shape
# Out[150]: torch.Size([4, 1, 28, 28, 1])

# In [151]: a.unsqueeze(-4).shape
# Out[151]: torch.Size([4, 1, 1, 28, 28])

# In [152]: a.unsqueeze(4).shape
# Out[152]: torch.Size([4, 1, 28, 28, 1])

# In [153]: a.unsqueeze(-5).shape
# Out[153]: torch.Size([1, 4, 1, 28, 28])

# In [154]: b=torch.rand(32)

# In [155]: f=torch.rand(4,32,14,14)

# In [156]: b=b.unsqueeze(1).unsqueeze(2).unsqueeze(0)

# In [157]: b.shape
# Out[157]: torch.Size([1, 32, 1, 1])

# squeeze
# In [158]: b.shape
# Out[158]: torch.Size([1, 32, 1, 1])

# In [159]: b.squeeze().shape
# Out[159]: torch.Size([32])

# In [160]: b.squeeze(0).shape
# Out[160]: torch.Size([32, 1, 1])

# In [161]: b.squeeze(-1).shape
# Out[161]: torch.Size([1, 32, 1])

# In [162]: b.squeeze(1).shape
# Out[162]: torch.Size([1, 32, 1, 1])

# In [163]: b.squeeze(-4).shape
# Out[163]: torch.Size([32, 1, 1])

# In [164]: a=torch.rand(4,32,14,14)

# expand
# In [165]: b.shape
# Out[165]: torch.Size([1, 32, 1, 1])

# In [166]: b.expand(4,32,14,14).shape
# Out[166]: torch.Size([4, 32, 14, 14])

# In [167]: b.expand(-1,32,-1,-1).shape
# Out[167]: torch.Size([1, 32, 1, 1])

# In [168]: b.expand(-1,32,-1,-4).shape
# Out[168]: torch.Size([1, 32, 1, -4])  # 这是一个bug，最新版本已经被facebook修复了

# repeat
# In [169]: b.shape
# Out[169]: torch.Size([1, 32, 1, 1])

# In [170]: b.repeat(4,32,1,1).shape
# Out[170]: torch.Size([4, 1024, 1, 1])

# In [171]: b.repeat(4,1,1,1).shape
# Out[171]: torch.Size([4, 32, 1, 1])

# In [172]: b.repeat(4,1,14,14).shape
# Out[172]: torch.Size([4, 32, 14, 14])

# 矩阵的转置
# In [176]: a.t()
# Out[176]: 
# tensor([[ 0.4365,  0.7924, -1.8549],
#         [ 0.7852,  1.6893,  0.2951],
#         [-1.0478, -1.2441, -0.4106],
#         [ 0.0849, -0.8694, -0.2315]])

# In [177]: a
# Out[177]: 
# tensor([[ 0.4365,  0.7852, -1.0478,  0.0849],
#         [ 0.7924,  1.6893, -1.2441, -0.8694],
#         [-1.8549,  0.2951, -0.4106, -0.2315]])

# 维度的交换transpose
# In [181]: a=torch.rand(4,3,32,32)

# In [182]: a2=a.transpose(1,3).contiguous().view(4,3*32*32).view(4,32,32,3).transpose(1,3)

# In [183]: torch.all(torch.eq(a,a2))
# Out[183]: tensor(True)

# In [184]: a=torch.rand(4,3,28,28)

# In [185]: a.transpose(1,3).shape
# Out[185]: torch.Size([4, 28, 28, 3])

# In [187]: b=torch.rand(4,3,28,32)

# In [188]: b.transpose(1,3).shape
# Out[188]: torch.Size([4, 32, 28, 3])

# In [189]: b.transpose(1,3).transpose(1,2).shape
# Out[189]: torch.Size([4, 28, 32, 3])

# In [190]: b.permute(0,2,3,1).shape
# Out[190]: torch.Size([4, 28, 32, 3])

# broadcast 广播
# In [191]: a.shape
# Out[191]: torch.Size([4, 3, 28, 28])

# In [192]: b=torch.rand(1,1,1,1)

# In [193]: b
# Out[193]: tensor([[[[0.6645]]]])

# In [194]: a+b
# Out[194]: 
# tensor([[[[1.4269, 0.9828, 1.4396,  ..., 1.0301, 1.1157, 0.7552],
#           [0.9599, 1.2660, 0.7905,  ..., 0.7814, 1.5358, 0.8884],
#           [1.0711, 1.0786, 1.2887,  ..., 0.9269, 0.6703, 1.6241],
#           ...,
#           [0.8904, 0.9374, 1.1382,  ..., 1.6495, 1.4969, 1.1237],
#           [1.4622, 1.4909, 0.8424,  ..., 1.3060, 1.3022, 1.5465],
#           [1.4041, 0.9195, 1.4952,  ..., 1.4560, 0.8696, 1.3523]],

#          [[0.7952, 0.8026, 1.3478,  ..., 0.9879, 1.6257, 1.6049],
#           [1.5786, 1.4707, 1.0197,  ..., 1.1306, 0.9951, 1.5886],
#           [0.7123, 1.6189, 1.0263,  ..., 1.0153, 1.5094, 0.7912],
#           ...,
#           [0.8667, 1.6460, 1.2174,  ..., 1.1893, 1.4029, 0.7018],
#           [0.8766, 0.8151, 1.2839,  ..., 1.0508, 1.2534, 1.5822],
#           [1.2128, 0.7407, 0.6863,  ..., 1.2233, 1.0471, 0.7367]],

#          [[1.2381, 1.6124, 1.1225,  ..., 1.2182, 1.6074, 1.4745],
#           [1.5595, 1.2213, 1.2152,  ..., 1.3256, 1.0363, 1.2919],
#           [1.3798, 0.8784, 1.3518,  ..., 1.6314, 1.5654, 0.9154],
#           ...,
#           [0.8938, 1.0472, 1.2522,  ..., 1.6226, 1.5695, 0.6847],
#           [0.6889, 0.7376, 1.2933,  ..., 1.1699, 0.6807, 1.2349],
#           [0.7892, 1.6136, 1.0518,  ..., 0.8087, 1.2614, 1.5153]]],


#         [[[1.0342, 1.5808, 1.1679,  ..., 1.6217, 0.7349, 1.0505],
#           [1.4348, 1.0631, 1.2416,  ..., 0.8293, 1.4239, 1.0260],
#           [1.1694, 0.8437, 0.8617,  ..., 1.4286, 1.2214, 1.1839],
#           ...,
#           [0.8934, 1.3616, 1.0873,  ..., 0.8992, 1.3667, 1.4319],
#           [0.8809, 1.1146, 0.8577,  ..., 1.2423, 1.4472, 0.9625],
#           [0.9183, 1.3877, 1.5510,  ..., 0.7255, 0.9400, 1.3499]],

#          [[1.1215, 1.1808, 1.2443,  ..., 0.6827, 1.0099, 1.1627],
#           [0.8392, 1.3818, 0.9762,  ..., 0.9882, 1.0745, 1.5426],
#           [0.6845, 1.2917, 0.7141,  ..., 1.5906, 1.2228, 1.6449],
#           ...,
#           [1.3368, 0.6817, 1.2342,  ..., 0.8543, 1.1787, 0.9608],
#           [1.0580, 1.1339, 1.6127,  ..., 0.9027, 1.0096, 1.4312],
#           [0.9190, 1.6491, 1.2456,  ..., 1.3501, 1.4089, 1.5196]],

#          [[0.9857, 1.0722, 1.4547,  ..., 1.0419, 1.5629, 0.6833],
#           [0.8961, 1.1554, 1.0683,  ..., 1.3014, 1.2232, 0.8186],
#           [1.1845, 1.0016, 1.5686,  ..., 1.6292, 0.6920, 1.4532],
#           ...,
#           [0.7639, 0.8240, 0.6926,  ..., 1.0729, 1.5771, 1.3421],
#           [0.9492, 0.7506, 0.8174,  ..., 0.8612, 1.1903, 0.8365],
#           [1.3611, 0.9252, 1.5108,  ..., 1.4283, 1.5446, 1.6510]]],


#         [[[1.3241, 0.8534, 1.2239,  ..., 1.2891, 1.2504, 1.5170],
#           [0.8910, 1.1941, 0.8449,  ..., 0.8689, 0.9442, 1.0034],
#           [1.1941, 0.7684, 1.0792,  ..., 0.9763, 0.9619, 0.7293],
#           ...,
#           [1.5841, 1.5441, 0.7689,  ..., 1.1702, 1.1943, 1.3091],
#           [0.7019, 1.2090, 1.3372,  ..., 1.5035, 1.1594, 1.2846],
#           [1.0086, 1.4003, 1.6612,  ..., 1.4621, 0.9564, 0.8955]],

#          [[1.3538, 1.5971, 1.0353,  ..., 1.6348, 1.1542, 1.5921],
#           [1.1969, 1.2828, 0.7069,  ..., 0.7886, 1.1474, 1.4511],
#           [1.5830, 1.3004, 0.8044,  ..., 1.0011, 0.7581, 1.3144],
#           ...,
#           [1.0396, 1.3658, 0.7988,  ..., 0.9374, 1.4675, 1.4110],
#           [1.0833, 1.0058, 1.4355,  ..., 1.0028, 1.4004, 0.9953],
#           [1.1833, 0.7706, 0.8258,  ..., 0.9885, 1.6576, 1.4829]],

#          [[1.4580, 1.3737, 0.6976,  ..., 1.1430, 1.2587, 1.4645],
#           [0.7330, 1.3968, 1.4398,  ..., 1.2091, 1.6517, 0.9979],
#           [1.3307, 1.2695, 0.8265,  ..., 1.5724, 1.3897, 1.5822],
#           ...,
#           [0.9651, 1.4899, 1.5774,  ..., 1.5354, 1.1273, 1.1908],
#           [1.3637, 1.2980, 0.8820,  ..., 1.1039, 1.6172, 0.9744],
#           [1.3115, 1.1822, 0.8180,  ..., 1.0111, 1.4503, 1.5628]]],


#         [[[0.9854, 0.8752, 1.2387,  ..., 1.6254, 0.8054, 1.1095],
#           [1.6638, 1.3502, 1.4424,  ..., 1.0623, 0.8374, 1.5378],
#           [1.2759, 1.2337, 1.3996,  ..., 1.5332, 0.6697, 0.8765],
#           ...,
#           [1.5144, 0.8150, 1.1762,  ..., 0.7954, 1.1922, 0.9663],
#           [0.7987, 1.1342, 1.1656,  ..., 1.0773, 1.0134, 1.0343],
#           [1.4932, 0.8116, 1.0323,  ..., 1.3769, 0.8936, 0.8366]],

#          [[1.2942, 1.3622, 0.8676,  ..., 1.2730, 1.6600, 1.1803],
#           [1.0136, 1.3749, 0.9405,  ..., 1.6030, 1.1600, 0.7819],
#           [1.2146, 0.9536, 0.9516,  ..., 1.4068, 0.7048, 0.8103],
#           ...,
#           [1.4931, 1.1920, 0.9147,  ..., 1.6531, 1.1575, 1.6072],
#           [1.1919, 1.6339, 0.7712,  ..., 1.5069, 1.1142, 0.9408],
#           [1.0438, 0.7007, 1.0123,  ..., 1.5359, 0.6992, 1.2787]],

#          [[1.1757, 1.4662, 1.2982,  ..., 1.5095, 0.6938, 1.3402],
#           [1.6209, 1.5840, 0.7819,  ..., 1.1662, 1.3569, 0.8160],
#           [1.0789, 1.2141, 0.8289,  ..., 0.8407, 0.9287, 1.4904],
#           ...,
#           [1.3240, 1.4469, 0.6850,  ..., 0.7699, 1.4030, 0.7519],
#           [1.0186, 1.1791, 1.1652,  ..., 1.5497, 1.1624, 1.6579],
#           [1.3670, 1.2433, 1.5147,  ..., 0.6791, 1.3747, 0.8277]]]])

# In [195]: a
# Out[195]: 
# tensor([[[[0.7623, 0.3183, 0.7751,  ..., 0.3656, 0.4512, 0.0907],
#           [0.2954, 0.6015, 0.1260,  ..., 0.1169, 0.8713, 0.2238],
#           [0.4066, 0.4141, 0.6241,  ..., 0.2624, 0.0058, 0.9595],
#           ...,
#           [0.2259, 0.2728, 0.4737,  ..., 0.9850, 0.8324, 0.4592],
#           [0.7977, 0.8264, 0.1778,  ..., 0.6414, 0.6377, 0.8819],
#           [0.7396, 0.2549, 0.8307,  ..., 0.7915, 0.2050, 0.6878]],

#          [[0.1307, 0.1381, 0.6833,  ..., 0.3234, 0.9611, 0.9404],
#           [0.9141, 0.8062, 0.3551,  ..., 0.4661, 0.3306, 0.9241],
#           [0.0478, 0.9543, 0.3618,  ..., 0.3507, 0.8449, 0.1266],
#           ...,
#           [0.2022, 0.9815, 0.5528,  ..., 0.5248, 0.7384, 0.0373],
#           [0.2121, 0.1506, 0.6194,  ..., 0.3863, 0.5889, 0.9177],
#           [0.5483, 0.0762, 0.0218,  ..., 0.5587, 0.3826, 0.0721]],

#          [[0.5736, 0.9478, 0.4580,  ..., 0.5537, 0.9429, 0.8100],
#           [0.8950, 0.5568, 0.5506,  ..., 0.6611, 0.3718, 0.6274],
#           [0.7153, 0.2139, 0.6873,  ..., 0.9668, 0.9009, 0.2509],
#           ...,
#           [0.2292, 0.3827, 0.5877,  ..., 0.9581, 0.9050, 0.0202],
#           [0.0244, 0.0731, 0.6288,  ..., 0.5054, 0.0162, 0.5703],
#           [0.1247, 0.9491, 0.3873,  ..., 0.1442, 0.5969, 0.8508]]],


#         [[[0.3696, 0.9162, 0.5034,  ..., 0.9572, 0.0704, 0.3860],
#           [0.7703, 0.3986, 0.5771,  ..., 0.1647, 0.7594, 0.3615],
#           [0.5049, 0.1791, 0.1972,  ..., 0.7640, 0.5568, 0.5194],
#           ...,
#           [0.2288, 0.6971, 0.4228,  ..., 0.2347, 0.7022, 0.7674],
#           [0.2164, 0.4500, 0.1932,  ..., 0.5778, 0.7827, 0.2980],
#           [0.2538, 0.7231, 0.8865,  ..., 0.0609, 0.2755, 0.6854]],

#          [[0.4569, 0.5163, 0.5798,  ..., 0.0182, 0.3454, 0.4982],
#           [0.1747, 0.7172, 0.3117,  ..., 0.3236, 0.4100, 0.8781],
#           [0.0200, 0.6272, 0.0496,  ..., 0.9260, 0.5583, 0.9804],
#           ...,
#           [0.6723, 0.0172, 0.5697,  ..., 0.1897, 0.5141, 0.2962],
#           [0.3935, 0.4693, 0.9481,  ..., 0.2382, 0.3451, 0.7667],
#           [0.2545, 0.9845, 0.5811,  ..., 0.6856, 0.7443, 0.8551]],

#          [[0.3212, 0.4077, 0.7902,  ..., 0.3774, 0.8984, 0.0188],
#           [0.2316, 0.4909, 0.4038,  ..., 0.6369, 0.5586, 0.1541],
#           [0.5200, 0.3370, 0.9041,  ..., 0.9646, 0.0275, 0.7887],
#           ...,
#           [0.0994, 0.1595, 0.0280,  ..., 0.4084, 0.9126, 0.6776],
#           [0.2847, 0.0861, 0.1529,  ..., 0.1967, 0.5258, 0.1719],
#           [0.6966, 0.2607, 0.8463,  ..., 0.7637, 0.8801, 0.9864]]],


#         [[[0.6596, 0.1889, 0.5594,  ..., 0.6246, 0.5859, 0.8525],
#           [0.2264, 0.5296, 0.1804,  ..., 0.2043, 0.2797, 0.3389],
#           [0.5296, 0.1039, 0.4147,  ..., 0.3117, 0.2974, 0.0648],
#           ...,
#           [0.9196, 0.8796, 0.1044,  ..., 0.5056, 0.5298, 0.6446],
#           [0.0373, 0.5445, 0.6727,  ..., 0.8390, 0.4948, 0.6201],
#           [0.3440, 0.7358, 0.9966,  ..., 0.7976, 0.2918, 0.2309]],

#          [[0.6893, 0.9325, 0.3708,  ..., 0.9703, 0.4897, 0.9275],
#           [0.5324, 0.6182, 0.0424,  ..., 0.1240, 0.4829, 0.7865],
#           [0.9185, 0.6359, 0.1399,  ..., 0.3366, 0.0936, 0.6499],
#           ...,
#           [0.3751, 0.7013, 0.1343,  ..., 0.2729, 0.8030, 0.7465],
#           [0.4188, 0.3412, 0.7710,  ..., 0.3382, 0.7358, 0.3307],
#           [0.5188, 0.1061, 0.1613,  ..., 0.3240, 0.9931, 0.8184]],

#          [[0.7934, 0.7092, 0.0331,  ..., 0.4784, 0.5942, 0.8000],
#           [0.0684, 0.7322, 0.7752,  ..., 0.5446, 0.9871, 0.3333],
#           [0.6662, 0.6050, 0.1620,  ..., 0.9078, 0.7252, 0.9177],
#           ...,
#           [0.3006, 0.8254, 0.9129,  ..., 0.8709, 0.4628, 0.5263],
#           [0.6992, 0.6335, 0.2175,  ..., 0.4394, 0.9527, 0.3099],
#           [0.6470, 0.5177, 0.1535,  ..., 0.3465, 0.7858, 0.8983]]],


#         [[[0.3209, 0.2106, 0.5741,  ..., 0.9608, 0.1408, 0.4450],
#           [0.9992, 0.6857, 0.7779,  ..., 0.3978, 0.1728, 0.8733],
#           [0.6114, 0.5691, 0.7351,  ..., 0.8686, 0.0052, 0.2120],
#           ...,
#           [0.8499, 0.1504, 0.5117,  ..., 0.1309, 0.5277, 0.3018],
#           [0.1342, 0.4697, 0.5010,  ..., 0.4127, 0.3489, 0.3698],
#           [0.8287, 0.1470, 0.3677,  ..., 0.7124, 0.2291, 0.1721]],

#          [[0.6297, 0.6977, 0.2031,  ..., 0.6085, 0.9954, 0.5158],
#           [0.3491, 0.7104, 0.2760,  ..., 0.9385, 0.4955, 0.1174],
#           [0.5501, 0.2891, 0.2870,  ..., 0.7423, 0.0402, 0.1457],
#           ...,
#           [0.8286, 0.5274, 0.2502,  ..., 0.9886, 0.4930, 0.9426],
#           [0.5274, 0.9694, 0.1066,  ..., 0.8424, 0.4497, 0.2763],
#           [0.3793, 0.0362, 0.3478,  ..., 0.8714, 0.0347, 0.6142]],

#          [[0.5112, 0.8017, 0.6337,  ..., 0.8450, 0.0292, 0.6757],
#           [0.9564, 0.9195, 0.1174,  ..., 0.5017, 0.6924, 0.1515],
#           [0.4144, 0.5496, 0.1644,  ..., 0.1761, 0.2642, 0.8258],
#           ...,
#           [0.6595, 0.7824, 0.0204,  ..., 0.1054, 0.7384, 0.0874],
#           [0.3541, 0.5146, 0.5007,  ..., 0.8852, 0.4979, 0.9934],
#           [0.7024, 0.5788, 0.8501,  ..., 0.0146, 0.7102, 0.1631]]]])

# 拼接与拆分
# cat stack split chunk
# In [196]: a=torch.rand(4,32,8)

# In [197]: b=torch.rand(5,32,8)

# In [198]: torch.cat([a,b],dim=0).shape
# Out[198]: torch.Size([9, 32, 8])

# In [199]: a1=torch.rand(4,3,32,32)

# In [200]: a2=torch.rand(5,3,32,32)

# In [201]: torch.cat([a1,a2],dim=0).shape
# Out[201]: torch.Size([9, 3, 32, 32])

# In [202]: a2=torch.rand(4,1,32,32)

# In [203]: torch.cat([a1,a2],dim=1).shape
# Out[203]: torch.Size([4, 4, 32, 32])

# In [204]: a1=torch.rand(4,3,16,32)

# In [205]: a2=torch.rand(4,3,16,32)

# In [206]: torch.cat([a1,a2],dim=2).shape
# Out[206]: torch.Size([4, 3, 32, 32])

# In [207]: a1.shape
# Out[207]: torch.Size([4, 3, 16, 32])

# In [208]: a2.shape
# Out[208]: torch.Size([4, 3, 16, 32])

# In [209]: torch.stack([a1,a2],dim=2).shape
# Out[209]: torch.Size([4, 3, 2, 16, 32])

# In [210]: a=torch.rand(32,8)

# In [211]: b=torch.rand(32,8)

# In [212]: torch.stack([a,b],dim=0).shape
# Out[212]: torch.Size([2, 32, 8])

# In [213]: c=torch.stack([a,b],dim=0).shape

# In [214]: c.shape

# In [215]: c=torch.stack([a,b],dim=0)

# In [216]: c.shape
# Out[216]: torch.Size([2, 32, 8])

# In [217]: aa, bb=c.split([1,1],dim=0)

# In [218]: aa.shape
# Out[218]: torch.Size([1, 32, 8])

# In [219]: bb.shape
# Out[219]: torch.Size([1, 32, 8])

# In [220]: aa,bb=c.chunk(2,dim=0)

# In [221]: aa.shape,bb.shape
# Out[221]: (torch.Size([1, 32, 8]), torch.Size([1, 32, 8]))

# 数学运算
# add/minus/multiply/divide
# matmul pow sqrt/rsqrt round

# In [222]: a=torch.rand(3,4)

# In [223]: b=torch.rand(4)

# In [224]: a+b
# Out[224]: 
# tensor([[1.0495, 0.9413, 0.7216, 1.5555],
#         [1.5433, 0.2319, 0.9659, 1.1997],
#         [1.5758, 0.1599, 0.5179, 1.5806]])

# In [225]: torch.add(a,b)
# Out[225]: 
# tensor([[1.0495, 0.9413, 0.7216, 1.5555],
#         [1.5433, 0.2319, 0.9659, 1.1997],
#         [1.5758, 0.1599, 0.5179, 1.5806]])

# In [226]: a
# Out[226]: 
# tensor([[0.3160, 0.9334, 0.2426, 0.5808],
#         [0.8097, 0.2240, 0.4868, 0.2250],
#         [0.8422, 0.1520, 0.0388, 0.6060]])

# In [227]: b
# Out[227]: tensor([0.7335, 0.0079, 0.4790, 0.9747])

# In [228]: torch.all(torch.eq(a-b,torch.sub(a,b)))
# Out[228]: tensor(True)

# In [229]: torch.all(torch.eq(a*b,torch.mul(a,b)))
# Out[229]: tensor(True)

# In [230]: a*b
# Out[230]: 
# tensor([[0.2318, 0.0074, 0.1162, 0.5661],
#         [0.5940, 0.0018, 0.2332, 0.2193],
#         [0.6178, 0.0012, 0.0186, 0.5906]])

# In [231]: torch.all(torch.eq(a/b,torch.div(a,b)))
# Out[231]: tensor(True)

# In [232]: a/b
# Out[232]: 
# tensor([[4.3075e-01, 1.1794e+02, 5.0651e-01, 5.9592e-01],
#         [1.1038e+00, 2.8308e+01, 1.0164e+00, 2.3082e-01],
#         [1.1482e+00, 1.9205e+01, 8.1100e-02, 6.2169e-01]])

# 矩阵相乘
# In [233]: a=torch.ones(2,2)*3

# In [234]: a
# Out[234]: 
# tensor([[3., 3.],
#         [3., 3.]])

# In [235]: b=torch.ones(2,2)

# In [236]: b
# Out[236]: 
# tensor([[1., 1.],
#         [1., 1.]])

# In [237]: torch.mm(a,b)
# Out[237]: 
# tensor([[6., 6.],
#         [6., 6.]])

# In [238]: torch.matmul(a,b)
# Out[238]: 
# tensor([[6., 6.],
#         [6., 6.]])

# In [239]: a@b
# Out[239]: 
# tensor([[6., 6.],
#         [6., 6.]])

# In [241]: x=torch.rand(4,784)

# In [242]: w=torch.rand(512,784)

# In [243]: 28*28
# Out[243]: 784

# In [244]: (x@w.t()).shape
# Out[244]: torch.Size([4, 512])

# 大于2维的矩阵乘法   : 后面两维使用矩阵乘法

# In [245]: a=torch.rand(4,3,28,64)

# In [246]: b=torch.rand(4,3,64,32)

# In [247]: torch.matmul(a,b).shape
# Out[247]: torch.Size([4, 3, 28, 32])

# In [248]: b=torch.rand(4,1,64,32)

# In [249]: torch.matmul(a,b).shape
# Out[249]: torch.Size([4, 3, 28, 32])


# power
# In [250]: a=torch.full([2,2],3)

# In [251]: a.pow(2)
# Out[251]: 
# tensor([[9., 9.],
#         [9., 9.]])

# In [252]: a**2
# Out[252]: 
# tensor([[9., 9.],
#         [9., 9.]])

# In [253]: aa=a**2

# In [254]: aa.sqrt()
# Out[254]: 
# tensor([[3., 3.],
#         [3., 3.]])

# In [255]: aa.rsqrt()
# Out[255]: 
# tensor([[0.3333, 0.3333],
#         [0.3333, 0.3333]])

# In [256]: aa**(0.5)
# Out[256]: 
# tensor([[3., 3.],
#         [3., 3.]])

# In [257]: a=torch.exp(torch.ones(2,2))

# In [258]: a
# Out[258]: 
# tensor([[2.7183, 2.7183],
#         [2.7183, 2.7183]])

# In [259]: torch.log(a)
# Out[259]: 
# tensor([[1., 1.],
#         [1., 1.]])

# In [261]: a.floor(),a.ceil(),a.trunc(),a.frac()
# Out[261]: (tensor(3.), tensor(4.), tensor(3.), tensor(0.1400))


# In [262]: grad=torch.rand(2,3)*15

# In [263]: grad.max()
# Out[263]: tensor(9.5293)

# In [264]: grad.median()
# Out[264]: tensor(4.7782)

# In [265]: grad.clamp(5)
# Out[265]: 
# tensor([[8.8639, 5.0000, 5.0000],
#         [5.0000, 9.5293, 9.3633]])

# In [266]: grad
# Out[266]: 
# tensor([[8.8639, 0.5865, 4.7782],
#         [4.1485, 9.5293, 9.3633]])

# In [267]: grad.clamp(0,10)
# Out[267]: 
# tensor([[8.8639, 0.5865, 4.7782],
#         [4.1485, 9.5293, 9.3633]])

# In [268]: a=torch.full([8],1)

# In [269]: b=a.view(2,4)

# In [270]: c=a.view(2,2,2)

# In [271]: b
# Out[271]: 
# tensor([[1., 1., 1., 1.],
#         [1., 1., 1., 1.]])

# In [273]: c
# Out[273]: 
# tensor([[[1., 1.],
#          [1., 1.]],

#         [[1., 1.],
#          [1., 1.]]])

# In [274]: a.norm(1)
# Out[274]: tensor(8.)

# In [275]: a.norm(1),b.norm(1),c.norm(1)
# Out[275]: (tensor(8.), tensor(8.), tensor(8.))

# In [276]: a
# Out[276]: tensor([1., 1., 1., 1., 1., 1., 1., 1.])

# In [277]: a.norm(2),b.norm(2),c.norm(2)
# Out[277]: (tensor(2.8284), tensor(2.8284), tensor(2.8284))

# In [278]: b.norm(1,dim=1)
# Out[278]: tensor([4., 4.])

# In [279]: b.norm(2,dim=1)
# Out[279]: tensor([2., 2.])

# In [280]: c.norm(1,dim=0)
# Out[280]: 
# tensor([[2., 2.],
#         [2., 2.]])

# In [281]: c.norm(2,dim=0)
# Out[281]: 
# tensor([[1.4142, 1.4142],
#         [1.4142, 1.4142]])

# In [282]: a=torch.arange(8).view(2,4).float()

# In [283]: a
# Out[283]: 
# tensor([[0., 1., 2., 3.],
#         [4., 5., 6., 7.]], dtype=torch.float32)

# In [284]: a.min(),a.max(),a.mean(),a.prod()
# Out[284]: 
# (tensor(0., dtype=torch.float32),
#  tensor(7., dtype=torch.float32),
#  tensor(3.5000, dtype=torch.float32),
#  tensor(0., dtype=torch.float32))

# In [285]: a.sum()
# Out[285]: tensor(28., dtype=torch.float32)

# In [286]: a.argmax(),a.argmin()
# Out[286]: (tensor(7), tensor(0))


# In [287]: a=a.view(1,2,4)

# In [288]: a.argmax()
# Out[288]: tensor(7)

# In [289]: a.argmin()
# Out[289]: tensor(0)

# In [290]: a=torch.rand(2,3,4)

# In [291]: a.argmax()
# Out[291]: tensor(0)

# In [292]: a
# Out[292]: 
# tensor([[[0.9960, 0.6065, 0.7414, 0.1638],
#          [0.5024, 0.8345, 0.6996, 0.1399],
#          [0.1871, 0.6877, 0.3522, 0.8092]],

#         [[0.8399, 0.4788, 0.7191, 0.1086],
#          [0.4452, 0.9300, 0.6395, 0.0559],
#          [0.4924, 0.3728, 0.6712, 0.6688]]])

# In [293]: a=torch.randn(4,10)

# In [294]: a[0]
# Out[294]: 
# tensor([-0.6503, -1.0677, -2.4234, -1.2887, -0.1077, -0.2430,  0.3335, -1.7614,
#         -0.5557, -1.4640])

# In [295]: a.argmax()
# Out[295]: tensor(30)

# In [296]: a.argmax(dim=1)
# Out[296]: tensor([6, 6, 4, 0])

# In [297]: a.max(dim=1)
# Out[297]: 
# torch.return_types.max(
# values=tensor([0.3335, 2.1502, 1.4372, 2.6872]),
# indices=tensor([6, 6, 4, 0]))

# In [298]: a.argmax(dim=1)
# Out[298]: tensor([6, 6, 4, 0])

# In [299]: a.max(dim=1,keepdim=True)
# Out[299]: 
# torch.return_types.max(
# values=tensor([[0.3335],
#         [2.1502],
#         [1.4372],
#         [2.6872]]),
# indices=tensor([[6],
#         [6],
#         [4],
#         [0]]))

# In [300]: a.argmax(dim=1,keepdim=True)
# Out[300]: 
# tensor([[6],
#         [6],
#         [4],
#         [0]])

# topk

# In [301]: a
# Out[301]: 
# tensor([[-0.6503, -1.0677, -2.4234, -1.2887, -0.1077, -0.2430,  0.3335, -1.7614,
#          -0.5557, -1.4640],
#         [-1.5392, -0.9415,  0.1876,  1.4121,  1.3292,  0.9029,  2.1502, -0.7266,
#           0.2302,  1.0754],
#         [-1.3697, -0.0413,  0.2963, -1.9574,  1.4372,  0.1498,  0.6835, -1.1469,
#          -0.7908, -0.5847],
#         [ 2.6872, -0.3766,  1.1221, -1.8567,  0.7693, -0.0937, -0.5921,  2.3118,
#          -0.0310,  0.4446]])

# In [302]: a.shape
# Out[302]: torch.Size([4, 10])

# In [303]: a.topk(3,dim=1)
# Out[303]: 
# torch.return_types.topk(
# values=tensor([[ 0.3335, -0.1077, -0.2430],
#         [ 2.1502,  1.4121,  1.3292],
#         [ 1.4372,  0.6835,  0.2963],
#         [ 2.6872,  2.3118,  1.1221]]),
# indices=tensor([[6, 4, 5],
#         [6, 3, 4],
#         [4, 6, 2],
#         [0, 7, 2]]))

# In [304]: a.topk(3,dim=1,largest=False)
# Out[304]: 
# torch.return_types.topk(
# values=tensor([[-2.4234, -1.7614, -1.4640],
#         [-1.5392, -0.9415, -0.7266],
#         [-1.9574, -1.3697, -1.1469],
#         [-1.8567, -0.5921, -0.3766]]),
# indices=tensor([[2, 7, 9],
#         [0, 1, 7],
#         [3, 0, 7],
#         [3, 6, 1]]))

# kthvalue
# In [305]: a.kthvalue(8,dim=1)
# Out[305]: 
# torch.return_types.kthvalue(
# values=tensor([-0.2430,  1.3292,  0.2963,  1.1221]),
# indices=tensor([5, 4, 2, 2]))

# In [306]: a>0
# Out[306]: 
# tensor([[False, False, False, False, False, False,  True, False, False, False],
#         [False, False,  True,  True,  True,  True,  True, False,  True,  True],
#         [False, False,  True, False,  True,  True,  True, False, False, False],
#         [ True, False,  True, False,  True, False, False,  True, False,  True]])

# In [307]: torch.gt(a,0)
# Out[307]: 
# tensor([[False, False, False, False, False, False,  True, False, False, False],
#         [False, False,  True,  True,  True,  True,  True, False,  True,  True],
#         [False, False,  True, False,  True,  True,  True, False, False, False],
#         [ True, False,  True, False,  True, False, False,  True, False,  True]])

# In [308]: a!=0
# Out[308]: 
# tensor([[True, True, True, True, True, True, True, True, True, True],
#         [True, True, True, True, True, True, True, True, True, True],
#         [True, True, True, True, True, True, True, True, True, True],
#         [True, True, True, True, True, True, True, True, True, True]])

# In [309]: a=torch.ones(2,3)

# In [310]: b=torch.randn(2,3)

# In [311]: torch.eq(a,b)
# Out[311]: 
# tensor([[False, False, False],
#         [False, False, False]])

# In [312]: torch.eq(a,a)
# Out[312]: 
# tensor([[True, True, True],
#         [True, True, True]])

# In [313]: torch.equal(a,a)
# Out[313]: True